use "Output Data\contracts_analysis_public.dta", clear

rename *, lower
drop if year < 2000

egen comp_agency = group(companyid agencyid)
tostring naics, gen(naics_str)
gen naics4_str = substr(naics_str,1,4)
destring naics4_str, gen(naics4)
tsset comp_agency year

gen event = 0
replace event = 1 if appointment == 1
replace event = 0 if year < 2000
replace event = 0 if year > 2018
bysort comp_agency: egen first_event_drop = min(year) if event == 1
bysort comp_agency: egen first_event = mean(first_event_drop)
drop first_event_drop
bysort comp_agency: egen first_year = min(year) if !missing(first_event)
replace first_year = 2000 if first_year < 2000
replace event = . if first_year > first_event - 2 & !missing(first_event) /*Psuedo drop these observations*/

gen event_tm1 = 0 if !missing(event)
replace event_tm1 = 1 if F.event == 1 & !missing(event)

bysort companyid: egen treatment = max(event)

drop if treatment == 1 & event_tm1 ~= 1
bysort companyid year: drop if treatment == 0 & _n ~= 1


gen ln_at = ln(at)
winsor2 ln_at roa q, cuts(1 99) replace

foreach x in ln_at roa q {
	egen z`x' = std(`x') if year <= 2018
}

tsset comp_agency year

teffects nnmatch (zln_at zroa zq) (treatment), ematch(naics4 year) metric(euclidean) generate(match_) osample(no_nn)
/* Previous line produces error for no nearest neighbor.  Restart code at line below */
teffects nnmatch (zln_at zroa zq) (treatment) if no_nn == 0, ematch(naics4 year) metric(euclidean) generate(match_) atet vce(iid)

gen ref = _n

keep ref companyid match_1

save "Output Data\contracts_analysis_public_matchedevents.dta", replace

drop if missing(match_1)
drop ref
rename match_1 ref
rename companyid companyid_treat

merge m:1 ref using "Output Data\contracts_analysis_public_matchedevents.dta"

keep if _merge == 3
drop match_1 ref _merge

stack companyid_treat companyid, into(companyid)

drop _stack
duplicates drop
sort companyid

save "Output Data\contracts_analysis_public_nnmset.dta", replace